﻿//using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;

public class Standard_Info : ICloneable
{

    public object Clone()
    {
        return MemberwiseClone();
    }
}

public class Standard_Controller : IDisposable
{
    private string _Transaction_Name;

    private static Dictionary<string, DataTable> m_CacheTable = new Dictionary<string, DataTable>();
    public static void CacheTable_Set(string sKey, DataTable dt)
    {
        sKey = sKey.ToUpper();
        if (m_CacheTable.ContainsKey(sKey))
        {
            m_CacheTable[sKey] = dt.Copy();
        }
        else
        {
            m_CacheTable.Add(sKey, dt.Copy());
        }
    }

    public static DataTable CacheTable_Get(string sKey)
    {
        sKey = sKey.ToUpper();
        if (m_CacheTable.ContainsKey(sKey))
        {
            return m_CacheTable[sKey].Copy();
        }
        else
        {
            return null;
        }
    }

    public static void CacheFile_Set(string sKey, DataTable dt)
    {
        sKey = sKey.ToUpper();
        //sKey = CryptoHelper.GenerateHash(CryptoHelper.AlgorithmHash.MD5, sKey);
        string sPath = "";// Application.ExecutablePath + "\\Cache";
        if (!System.IO.Directory.Exists(sPath))
        {
            System.IO.Directory.CreateDirectory(sPath);
        }
        string sFile = sPath + "\\" + sKey + ".bin";
        if (System.IO.File.Exists(sFile))
        {
            System.IO.File.Delete(sFile);
        }
        System.IO.FileStream fs = new System.IO.FileStream(sFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
        System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
        bw.Write(Globals.DataTable_Serialize(dt));
        bw.Close();
    }

    public static DataTable CacheFile_Get(string sKey)
    {
        return CacheFile_Get(sKey, System.DateTime.MinValue);
    }

    public static DataTable CacheFile_Get(string sKey, System.DateTime dLastUpdate)
    {
        sKey = sKey.ToUpper();
        //sKey = CryptoHelper.GenerateHash(CryptoHelper.AlgorithmHash.MD5, sKey);
        string sPath = "";// Application.ExecutablePath + "\\Cache";
        if (!System.IO.Directory.Exists(sPath))
        {
            return null;
        }
        string sFile = sPath + "\\" + sKey + ".bin";
        if (!System.IO.File.Exists(sFile))
        {
            return null;
        }
        else
        {
            if (System.IO.File.GetLastWriteTime(sFile) < dLastUpdate)
            {
                return null;
            }
        }
        System.IO.FileStream fs = new System.IO.FileStream(sFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
        System.IO.BinaryReader bw = new System.IO.BinaryReader(fs);
        byte[] buffer = null;
        buffer = bw.ReadBytes((Int32)fs.Length);
        bw.Close();
        //
        return Globals.DataTable_Deserialize(buffer);
    }

    public string Procedure_Name(string tableName, string functionName,bool appendTable = true)
    {
        if (String.IsNullOrEmpty(tableName) || !appendTable)
            return functionName;
        else
        {
            string retVal = tableName.Replace("Dic_", "").Replace("App_", "");
            if ((retVal + "_" + functionName).Length > 30 && retVal.LastIndexOf("_") < 10)
            {
                retVal = retVal.Substring(0, retVal.LastIndexOf("_"));
            }
            //retVal = "PCK_" + tableName.ToUpper() + "." + retVal + "_" + functionName;
            retVal = retVal + "_" + functionName;
            return retVal;
        }
    }

    public string Procedure_Name2(string tableName, string functionName)
    {
        string retVal = "SP";
        retVal = "PCK_" + tableName.ToUpper() + "." + retVal + "_" + functionName.ToUpper();
        return retVal;
    }

    public System.DateTime Table_LastDML(string sTableName)
    {
        //Purpose: 
        //   Lấy ngay cap nhat du lieu của 1 table
        //Parameters: 
        //   sTableName : tên bảng dữ liệu
        //Notes:
        //   Thường dùng khi cache du lieu cua cac bang Danh muc
        return Globals.DB_GetValue(DataConnect.ExecuteScalar(DataConnect.WorkingConnect, "App_Table_LastDML", sTableName.ToUpper()), Null.MIN_DATE);
    }

    public void Table_UpdateDML(string sTableName)
    {
        //Purpose: 
        //   Cap nhat ngay thay doi của 1 table
        //Parameters: 
        //   sTableName : tên bảng dữ liệu
        //Notes:
        //   Thường dùng khi cache du lieu cua cac bang Danh muc
        DataConnect.ExecuteNonQuery(DataConnect.WorkingConnect, "App_Table_UpdateDML", sTableName.ToUpper());
    }

    public string Trans_Name
    {
        get
        {
            if (!DataConnect.Check_Trans(ref _Transaction_Name))
            {
                _Transaction_Name = "";
            }
            return _Transaction_Name;
        }
        set { _Transaction_Name = value; }
    }

    public void Begin_Trans()
    {
        _Transaction_Name = DataConnect.Begin_Trans();
    }

    public void Rollback_Trans()
    {
        DataConnect.Rollback_Trans(_Transaction_Name);
        _Transaction_Name = "";
    }

    public void Commit_Trans()
    {
        DataConnect.Commit_Trans(_Transaction_Name);
        _Transaction_Name = "";
    }
    ////
    //  Wrapper method for support transaction 
    public void ExecuteNonQuery(string commandText)
    {
        DataConnect.ExecuteNonQuery((int)DataConnect.GetConnectType(Trans_Name), CommandType.Text, commandText);
    }

    public void ExecuteNonQuery(string spName, params object[] parameterValues)
    {
        DataConnect.ExecuteNonQuery((int)DataConnect.GetConnectType(Trans_Name), spName, parameterValues);
    }

    public void ExecuteNonQuery(string spName, object[][] parameterArray)
    {
        DataConnect.ExecuteNonQuery((int)DataConnect.GetConnectType(Trans_Name), spName, parameterArray);
    }

    public object ExecuteScalar(string commandText)
    {
        return DataConnect.ExecuteScalar((int)DataConnect.GetConnectType(Trans_Name), CommandType.Text, commandText);
    }

    public object ExecuteScalar(string spName, params object[] parameterValues)
    {
        return DataConnect.ExecuteScalar((int)DataConnect.GetConnectType(Trans_Name), spName, parameterValues);
    }

    public object ExecuteScalar(string spName, object[][] parameterArray)
    {
        return DataConnect.ExecuteScalar((int)DataConnect.GetConnectType(Trans_Name), spName, parameterArray);
    }

    public DataRowEx ExecuteRow(string spName, params object[] parameterValues)
    {
        return DataConnect.ExecuteRowEx((int)DataConnect.GetConnectType(Trans_Name), spName, parameterValues);
    }

    public DataTable ExecuteTable(string spName, params object[] parameterValues)
    {
        return DataConnect.ExecuteTable((int)DataConnect.GetConnectType(Trans_Name), spName, parameterValues);
    }

    public DataSet ExecuteDataset(string spName, params object[] parameterValues)
    {
        return DataConnect.ExecuteDataset((int)DataConnect.GetConnectType(Trans_Name), spName, parameterValues);
    }

    ////---------------------------------
    // To detect redundant calls
    private bool disposedValue = false;

    // IDisposable
    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposedValue)
        {
            if (disposing)
            {
                // TODO: free other state (managed objects).
            }

            // TODO: free your own state (unmanaged objects).
            // TODO: set large fields to null.
        }
        this.disposedValue = true;
    }

    #region " IDisposable Support "
    // This code added by Visual Basic to correctly implement the disposable pattern.
    public void Dispose()
    {
        // Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        Dispose(true);
        GC.SuppressFinalize(this);
    }
    #endregion

}